package com.ufaud.ais.sys.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.ufaud.ais.pub.entity.BaseEntity;

/**
 * 
 * @author chenjd
 * 
 */

@Entity
@Table(name = "department")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries( { @NamedQuery(name = "getDeptTree", query = "select d from Department d  where d.superDept is null") })
public class Department extends BaseEntity<Long> {

	@Id
	@GeneratedValue
	@Column(name = "dept_id")
	private Long deptId;

	@Column(name = "dept_name")
	private String deptName;

	@Column(name = "dept_desc")
	private String deptDesc;

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = Department.class)
	@JoinColumn(name = "super_dept_id")
	private Department superDept;

	@OneToMany(fetch = FetchType.LAZY, targetEntity = Department.class, mappedBy = "superDept")
	@Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
	private Set<Department> subDeptSet = new HashSet<Department>();

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = Employee.class)
	@JoinColumn(name = "dept_manager_id")
	private Employee deptManager;

	@OneToMany(fetch = FetchType.LAZY, targetEntity = Employee.class, mappedBy = "department")
	private Set<Employee> employeeSet = new HashSet<Employee>();

	public void recordTransfer() {

	}

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getDeptDesc() {
		return deptDesc;
	}

	public void setDeptDesc(String deptDesc) {
		this.deptDesc = deptDesc;
	}

	public Department getSuperDept() {
		return superDept;
	}

	public void setSuperDept(Department superDept) {
		this.superDept = superDept;
	}

	public Set<Department> getSubDeptSet() {
		return subDeptSet;
	}

	public void setSubDeptSet(Set<Department> subDeptSet) {
		this.subDeptSet = subDeptSet;
	}

	public Set<Employee> getEmployeeSet() {
		return employeeSet;
	}

	public void setEmployeeSet(Set<Employee> employeeSet) {
		this.employeeSet = employeeSet;
	}

	public void addEmployee(Employee employee) {
		employeeSet.add(employee);
	}

	public void addSubDepartment(Department subDepartment) {
		subDepartment.setSuperDept(this);
		subDeptSet.add(subDepartment);
	}

	public Employee getDeptManager() {
		return deptManager;
	}

	public void setDeptManager(Employee deptManager) {
		this.deptManager = deptManager;
	}

	@Override
	@Transient
	public String getPropertyNameOfId() {
		return "deptId";
	}

}
